package com.dsteiner.widgets.client;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class ObjectUtil {

	public static boolean equals(Object obj, Object other) {
		if(obj == null && other == null)
			return true;
		if(obj != null && other != null && obj.equals(other))
			return true;
		return false;
	}

	public static String getArrayAsString(Collection columns) {
		return getArrayAsString(columns, ";");
	}
	
	public static String getArrayAsString(Collection columns, String separator) {
		if(columns == null)
			return null;
		StringBuffer result = new StringBuffer();
		for (Iterator iterator = columns.iterator(); iterator.hasNext();) {
			Object column = iterator.next();
			result.append(column.toString());
			if(iterator.hasNext())
				result.append(separator);
		}
		return result.toString();
	}

	public static String getArrayAsString(String[] array, String separator) {
		if(array == null)
			return null;
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < array.length; i++) {
			String value = array[i];
			if(value != null && !"".equals(value))
				list.add(value);
		}
		return getArrayAsString(list, separator);
	}

	
	public static List<Integer> getStringAsListInt(String string, String separator) {
		List<Integer> list = new ArrayList<Integer>();
		String[] split = string.split(separator);
		for (int i = 0; i < split.length; i++) {
			list.add(new Integer(split[i]));
		}
		return list;
	}

	public static Set<Integer> getStringAsSetInt(String string, String separator) {
		Set<Integer> set = new HashSet<Integer>();
		String[] split = string.split(separator);
		for (int i = 0; i < split.length; i++) {
			set.add(new Integer(split[i]));
		}
		return set;
	}
	
	public static boolean isDouble(String text){
		try{
			double parseDouble = Double.parseDouble(text);
			return true;
		}catch (Exception e) {
		}
		return false;
	}
	public static boolean isNumber(String text){
		String[] splitWhiteSpace = text.split(" ");
		String[] splitDash = text.split("-");
		try{
			Integer number = new Integer(splitWhiteSpace[0]);
			if(number != null)
				return true;
			number = new Integer(splitDash[0]);
			if(number != null)
				return true;
		}catch(Exception ex){
		}
		return false;
	}

	public static <T> List<T> subList(List<T> list, int fromIndex, int toIndex) {
		List<T> result = new ArrayList<T>();
		for (int i = fromIndex; i < toIndex && i < list.size(); i++) {
			result.add(list.get(i));
		}
		return result;
	}

	public static int compare(Comparable value1, Comparable value2) {
		if(value1 == null && value2 == null)
			return 0;
		else if(value1 == null)
			return 1;
		else if(value2 == null)
			return -1;
		else
			return value1.compareTo(value2);
	}

}
